#!/usr/bin/env python
#
# Result handling pipeline filter: allows only results of specified types to
# pass through
#
# Author:   Mike Murphy <mamurph@cs.clemson.edu>
# Revision: 23 September 2009
#
#   Copyright 2009 Clemson University
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# This material is based upon work supported under a National Science
# Foundation Graduate Research Fellowship. Any opinions, findings,
# conclusions or recommendations expressed in this publication are those of
# the author(s) and do not necessarily reflect the views of the National
# Science Foundation.

'''
Output pipeline component that filters results by type, forwarding only the
requested types to the downstream portion of the pipeline
'''

from kernel import Output


class ResultFilter(Output):
   '''
   Output pipeline component that filters results by type. Result types are
   specified as a sequence of strings, giving the result type names as encoded
   within the result objects. Only results whose result type names match
   elements of the sequence will be forwarded to the downstream side of the
   pipeline.
   '''
   def __init__(self, result_selector, output_handler):
      '''
      @param result_selector: Sequence of strings containing result type names
      @param output_handler: Downstream output handler instance
      '''
      Output.__init__(self, output_handler)
      self.selector = result_selector
   #
   # Override docstrings on the handlers
   def handle_append(self, result):
      '''
      Does nothing in this implementation and is not called.
      '''
      pass
   #
   def handle_close(self):
      '''
      Does nothing in this implementation and is not called.
      '''
      pass
   #
   #
   def append(self, result):
      '''
      Sends a result to the downstream side of the pipeline if the result type
      matches one of the requested types in the selector. If the result type
      does not match, then the result is discarded.

      @param result: Input result
      '''
      if result.result_type in self.selector:
         self.output_handler.append(result)
   #
   def close(self):
      '''
      Closes all output handlers downstream of this component
      '''
      self.output_handler.close()
#
